package com.atguigu.controller;

import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @program:weiblog-server
 * @author: 马雨清
 * @Time: 2023/8/21  14:26
 * @description:
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
@Tag(name = "UserController")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result userLogin(@RequestBody User user){
        User loginUser = userService.login(user);
        Map<String,Object> userMap = new HashMap<>();
        if (null != loginUser){
            userMap.put("uid",loginUser.getUid());
            userMap.put("username",loginUser.getUsername());
            return Result.ok(userMap);
        }
        return Result.fail();
    }

    @Operation(summary = "根据id查询")
    @GetMapping("findUserById/{id}")
    public Result findById(@PathVariable Integer id){
        User user = userService.findById(id);
        if (null==user){
            return Result.fail();
        }
        return Result.ok(user);
    }

    @Operation(summary = "更新用户")
    @PutMapping("updateUser")
    public Result modifyUser(@RequestBody User user){
        System.out.println(user);
        Integer result = userService.modifyUser(user);
        if (result!=1){
            return Result.fail();
        }
        System.out.println("修改结果为："+result);
        return Result.ok();
    }
}
